//
//  SABatchPurchaseDetailHeaderView.m
//  saasRetail
//
//  Created by mac on 2022/10/10.
//

#import "SABatchPurchaseDetailHeaderView.h"
#import "SASelectImageView.h"
#import "UILabel+NSMutableAttributedString.h"
#import "SAOcrScanResultModel.h"
#import "GFAlertView.h"
#import "ljdrawrect.h"

@interface SABatchPurchaseDetailHeaderView ()<UITextFieldDelegate>

@property (nonatomic, strong) UILabel *productNameLab;
@property (nonatomic, strong) UILabel *productCodeLab;

@property (nonatomic, strong) UITextField *productNameTf;
@property (nonatomic, strong) UITextField *productCodeTf;
@property (nonatomic, strong) UITextField *priceTf;
@property (nonatomic, strong) UITextField *retailPriceTf;
@property (nonatomic, strong) UITextField *derivativePriceTf;
@property (nonatomic, strong) SASelectImageView *selectImageV;
@property (nonatomic, strong) UIView *colourSizeView;

@property (nonatomic, strong) UIButton *replenishmentBtn;
@property (nonatomic, strong) UIButton *addBtn;
@property (nonatomic, strong) UIImageView *imageV;

@end

@implementation SABatchPurchaseDetailHeaderView

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithReuseIdentifier:reuseIdentifier]) {
        [self initUI];
    }
    return self;
}

-(void)setModel:(SAOcrScanResultModel *)model {
    _model = model;
    self.productNameTf.text = model.productName;
    self.productCodeTf.text = model.productCode;
    self.priceTf.text = model.price;
    self.retailPriceTf.text = model.retailPrice;
    self.derivativePriceTf.text = model.microStorePrice;
    
    NSArray *imageArr = [model.imgUrl componentsSeparatedByString:@","];
    [self.selectImageV refreshData:imageArr AndType:model.productIsExist];
    
    self.colourSizeView.hidden = (model.skuList.count == 0);
    
    if ([model.productIsExist isEqualToString:@"1"]) {
        self.imageV.hidden = NO;
        self.productNameTf.userInteractionEnabled = NO;
        self.productCodeTf.userInteractionEnabled = NO;
        self.priceTf.userInteractionEnabled = NO;
        self.retailPriceTf.userInteractionEnabled = NO;
        self.derivativePriceTf.userInteractionEnabled = NO;
    
        self.productNameTf.clearButtonMode = UITextFieldViewModeNever;
        self.productCodeTf.clearButtonMode = UITextFieldViewModeNever;
        self.priceTf.clearButtonMode = UITextFieldViewModeNever;
        self.retailPriceTf.clearButtonMode = UITextFieldViewModeNever;
        self.derivativePriceTf.clearButtonMode = UITextFieldViewModeNever;
    }else {
        self.imageV.hidden = YES;
        self.productNameTf.userInteractionEnabled = YES;
        self.productCodeTf.userInteractionEnabled = YES;
        self.priceTf.userInteractionEnabled = YES;
        self.retailPriceTf.userInteractionEnabled = YES;
        self.derivativePriceTf.userInteractionEnabled = YES;
    
        self.productNameTf.clearButtonMode = UITextFieldViewModeAlways;
        self.productCodeTf.clearButtonMode = UITextFieldViewModeAlways;
        self.priceTf.clearButtonMode = UITextFieldViewModeAlways;
        self.retailPriceTf.clearButtonMode = UITextFieldViewModeAlways;
        self.derivativePriceTf.clearButtonMode = UITextFieldViewModeAlways;
    }
}

- (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string {
    if (string.length == 0) {
        return YES;
    }
    
    if (textField.tag == 100 || textField.tag == 101) {
        if (textField.text.length > 14) {
            return NO;
        }
    }else {
        if (textField.text.length > 7) {
            return NO;
        }
    }

    if (textField.tag == 100) {
        return YES;
    }
    
    if (textField.tag == 101) {
        return [self validateNumber:string];
    }
    
    BOOL isHaveDian = NO;
    if([textField.text containsString:@"."]) {
        isHaveDian = YES;
    }
    if(string.length > 0) {
        //当前输入的字符
        unichar single = [string characterAtIndex:0];
        // 不能输入.0-9以外的字符
        if(!((single >='0'&& single <='9') || single =='.')){
            return NO;
        }
        // 只能有一个小数点
        if(isHaveDian && single =='.') {
            return NO;
        }
        // 如果第一位是.则前面加上0.
        if((textField.text.length==0) && (single =='.')) {
            textField.text=@"0";
        }
        // 如果第一位是0则后面必须输入点，否则不能输入。
        if([textField.text hasPrefix:@"0"]) {
            if(textField.text.length>1) {
                NSString *secondStr = [textField.text substringWithRange:NSMakeRange(1,1)];
                if(![secondStr isEqualToString:@"."]) {
                    return NO;
                }
            }else{
                if(![string isEqualToString:@"."]) {
                    return NO;
                }
            }
        }
        // 小数点后最多能输入两位
        if(isHaveDian) {
            NSRange ran = [textField.text rangeOfString:@"."];
            if(range.location > ran.location) {
                if([textField.text pathExtension].length>1) {
                    return NO;
                }
            }
        }
    }
    return YES;
}

- (BOOL)validateNumber:(NSString*)number {
    BOOL res = YES;
    NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"/ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345678\\9*-"];
    int i = 0;
    while (i < number.length) {
        NSString * string = [number substringWithRange:NSMakeRange(i, 1)];
        NSRange range = [string rangeOfCharacterFromSet:tmpSet];
        if (range.length == 0) {
            res = NO;
            break;
        }
        i++;
    }
    return res;
}

- (void)textFieldEditChanged:(UITextField *)textField {
    if (self.productNameTf == textField) {
        self.model.productName = self.productNameTf.text;
    }else if (self.productCodeTf == textField) {
        self.model.productCode = self.productCodeTf.text;
    }else if (self.priceTf == textField) {
        self.model.price = self.priceTf.text;
        !self.changePriceBlock ?: self.changePriceBlock();
    }else if (self.retailPriceTf == textField) {
        self.model.retailPrice = self.retailPriceTf.text;
    }else if (self.derivativePriceTf == textField) {
        self.model.microStorePrice = self.derivativePriceTf.text;
    }
}

#pragma mark - evnet
-(void)clickedReplenishmentBtn {
    
}

-(void)clickedAddBtn {
    
}

-(void)clickeddTipBtn {
    
}

-(void)clickeddDeleteBtn {
    GFAlertView *alertView = [GFAlertView alertViewWithMsg:@"删除提醒" detailMsg:@"确定删除该商品吗？" knowBtnBlock:^(UIButton *knowBtn) {
        [knowBtn addTarget:self action:@selector(clickeddDeleteBtn2) forControlEvents:UIControlEventTouchUpInside];
    }];
    [alertView showWithAnimation];
}

-(void)clickeddDeleteBtn2 {
    !self.deleteBlock ?: self.deleteBlock();
}

#pragma mark - initUI
-(void)initUI {
    
    UIView *line2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScaleNum(8))];
    line2.backgroundColor = kLineColor;
    [self.contentView addSubview:line2];
    
    CGPoint piont1;
    piont1.x = 0;
    piont1.y = 0;
    
    CGPoint piont2;
    piont2.x = kScaleNum(90);
    piont2.y = 0;
    
    CGPoint piont3;
    piont3.x = 0;
    piont3.y = kScaleNum(39);
    
    ljdrawrect *_ljview = [[ljdrawrect alloc] initStartPoint:piont1 middlepoint:piont2 endpoint:piont3 color:kBuleColor];
    _ljview.frame = SARectMake(0, 8, 90, 40);
    [self.contentView addSubview:_ljview];
    
    UILabel *indexLab = [[UILabel alloc] initWithFrame:SARectMake(10, 8, 40, 30)];
    indexLab.font = kBFont(15);
    indexLab.textColor = [UIColor whiteColor];
    indexLab.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:indexLab];
    self.indexLab = indexLab;
    
    UIImageView *imageV = [[UIImageView alloc] initWithFrame:SARectMake(90, 18, 20, 20)];
    imageV.image = [UIImage imageNamed:@"sales_home_list_hit_1"];
    [self.contentView addSubview:imageV];
    self.imageV = imageV;
    self.imageV.hidden = YES;
    
//    UIButton *replenishmentBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    replenishmentBtn.frame = SARectMake((kBaseScreenWidth - 120)/2, 15, 60, 26);
//    [replenishmentBtn setTitle:@"补货" forState:UIControlStateNormal];
//    [replenishmentBtn setTitleColor:kBlackColor forState:UIControlStateNormal];
//    replenishmentBtn.backgroundColor = kColor(220,220,220,1.0);
//    replenishmentBtn.titleLabel.font = kFont(16);
//    replenishmentBtn.userInteractionEnabled = NO;
//    [replenishmentBtn addTarget:self action:@selector(clickedReplenishmentBtn) forControlEvents:UIControlEventTouchUpInside];
//    [self.contentView addSubview:replenishmentBtn];
//    self.replenishmentBtn = replenishmentBtn;
    
//    sales_home_list_hit_1

//    UIButton *addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    addBtn.frame = SARectMake((kBaseScreenWidth - 120)/2 + 60, 15, 60, 26);
//    [addBtn setTitle:@"新增" forState:UIControlStateNormal];
//    [addBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//    addBtn.backgroundColor = kBuleColor;
//    addBtn.titleLabel.font = kFont(16);
//    addBtn.userInteractionEnabled = NO;
//    [addBtn addTarget:self action:@selector(clickedAddBtn) forControlEvents:UIControlEventTouchUpInside];
//    [self.contentView addSubview:addBtn];
//    self.addBtn = addBtn;

//    UIButton *tipBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    tipBtn.frame = SARectMake((kBaseScreenWidth - 120)/2 + 120, 15, 40, 35);
//    [tipBtn setImage:[UIImage imageNamed:@"Shop2_?"] forState:UIControlStateNormal];
//    [tipBtn addTarget:self action:@selector(clickeddTipBtn) forControlEvents:UIControlEventTouchUpInside];
//    [self.contentView addSubview:tipBtn];
    
    UIButton *deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    deleteBtn.frame = SARectMake(kBaseScreenWidth - 60, 8, 60, 40);
    [deleteBtn setImage:[UIImage imageNamed:@"shop_delete"] forState:UIControlStateNormal];
    [deleteBtn addTarget:self action:@selector(clickeddDeleteBtn) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:deleteBtn];
    
    SASelectImageView *selectImageV = [[SASelectImageView alloc] initWithFrame:SARectMake(0, 48, kBaseScreenWidth, 75)];
    selectImageV.backgroundColor = kLineColor;
    [self.contentView addSubview:selectImageV];
    self.selectImageV = selectImageV;
    WeakSelf
    selectImageV.selectedBlock = ^(NSString * _Nonnull imageUrl) {
        weakSelf.model.imgUrl = imageUrl;
    };
    
    NSArray *arr = @[@"名称*", @"款号", @"进货价", @"实体零售价", @"微商城售价"];
    for (int i = 0; i < arr.count; i++) {
        UILabel *titleLab = [[UILabel alloc] initWithFrame:SARectMake(20, 123 + 41 * i, 100, 40)];
        titleLab.text = arr[i];
        titleLab.font = kFont(15);
        titleLab.textColor = kBlackColor;
        [self.contentView addSubview:titleLab];
        [titleLab setAttributedStringWithSubString:@"*" color:[UIColor redColor]];
        
        UITextField *contentTf = [[UITextField alloc] initWithFrame:SARectMake(kBaseScreenWidth - 220, 123 + 41 * i, 200, 40)];
        contentTf.text = @"";
        contentTf.font = kFont(15);
        contentTf.textColor = kBlackColor;
        contentTf.textAlignment = NSTextAlignmentRight;
        contentTf.clearButtonMode = UITextFieldViewModeAlways;
        contentTf.delegate = self;
        contentTf.tag = i + 100;
        [contentTf addTarget:self action:@selector(textFieldEditChanged:) forControlEvents:UIControlEventEditingChanged];
        [self.contentView addSubview:contentTf];
        
        if (i == 0) {
            self.productNameTf = contentTf;
            self.productNameLab = titleLab;
        }else if (i == 1) {
            self.productCodeTf = contentTf;
            self.productCodeLab = titleLab;
        }else if (i == 2) {
            self.priceTf = contentTf;
            contentTf.keyboardType = UIKeyboardTypeDecimalPad;
        }else if (i == 3) {
            self.retailPriceTf = contentTf;
            contentTf.keyboardType = UIKeyboardTypeDecimalPad;
        }else {
            self.derivativePriceTf = contentTf;
            contentTf.keyboardType = UIKeyboardTypeDecimalPad;
        }
        
        if (i != 4) {
            UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, (163 + 41 * i) * kScreenScale, kScreenWidth, 1)];
            line.backgroundColor = kLineColor;
            [self.contentView addSubview:line];
        }
    }
    
    UIView *colourSizeView = [[UIView alloc] initWithFrame:SARectMake(0, 327, kBaseScreenWidth, 41)];
    self.colourSizeView = colourSizeView;
    [self.contentView addSubview:colourSizeView];
    
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 1)];
    line.backgroundColor = kLineColor;
    [colourSizeView addSubview:line];
    
    NSArray *arr2 = @[@"颜色*", @"尺码*", @"数量*"];
    for (int i = 0; i < arr2.count; i++) {
        UILabel *titleLab = [[UILabel alloc] initWithFrame:SARectMake(125 * i, 0, 125, 40)];
        titleLab.text = arr2[i];
        titleLab.font = kBFont(16);
        titleLab.textAlignment = NSTextAlignmentCenter;
        titleLab.textColor = kBlackColor;
        [colourSizeView addSubview:titleLab];
        [titleLab setAttributedStringWithSubString:@"*" color:[UIColor redColor]];
    }
}

@end

